package com.signal;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;

public class Tela9 extends Activity{
	
	private TableLayout mTableLayout;
	private Button mButtonRemover;
	private Button mButtonAdicionar;
	private Button mButtonContinuar;
	
	private int mLinhaClicada = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.layout_tela9);
		
		
		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("		Lista Vazia		");
        alertbox.setNeutralButton("CONTINUAR", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                
            }
        });
        alertbox.show();
		
		
		mTableLayout = (TableLayout)findViewById(R.id.tela9_table_layout);
		mButtonAdicionar = (Button)findViewById(R.id.tela9_button_adicionar);
		mButtonRemover = (Button)findViewById(R.id.tela9_button_remover);
		mButtonContinuar = (Button)findViewById(R.id.tela9_button_continuar);
		
		mButtonAdicionar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				adicionarLinha("material 1", 3);
			}
			
		});
		
		mButtonAdicionar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//adicionarLinha("material 1", 3);
				Intent i = new Intent(Tela9.this,Tela9_2.class);
				startActivity(i);
				Tela9_2.sReference = Tela9.this;
			}
			
		});
		
		mButtonRemover.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mLinhaClicada != -1){
					removerLinha(mLinhaClicada);
					
					if(mTableLayout.getChildCount() == 0){
						AlertDialog.Builder alertbox = new AlertDialog.Builder(Tela9.this);
				        alertbox.setMessage("		Lista Vazia		");
				        alertbox.setNeutralButton("CONTINUAR", new DialogInterface.OnClickListener() {
				            public void onClick(DialogInterface arg0, int arg1) {
				                
				            }
				        });
				        alertbox.show();
					}
				}
			}
			
		});
		
		mButtonContinuar = (Button)findViewById(R.id.tela9_button_continuar);
		mButtonContinuar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Tela9.this, Tela10.class);
				startActivity(i);				
			}
			
		});
		
		
		
		
	}
	
	public void adicionarLinha(String nomeMaterial, int qtdMaterial){
		
		 final TableRow tr = new TableRow(this);
         tr.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        100));
         
         
         
              /* Create a Button to be the row-content. */
              TextView b = new TextView(this);
              b.setText(nomeMaterial);
              b.setWidth(300);
              b.setTextColor(Color.BLACK);
              
              TextView b2 = new TextView(this);
              b2.setText(""+qtdMaterial);
              b2.setWidth(30);
              b2.setTextColor(Color.BLACK);
              
              tr.addView(b);
              tr.addView(b2);
    
          tr.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				
				for(int x=0; x<mTableLayout.getChildCount(); x++){
					View linha = mTableLayout.getChildAt(x);
					linha.setBackgroundColor(Color.WHITE);
					//System.out.println("CHILD COUNT: " + mTableLayout.getChildCount());
					
					if(linha == tr){
						mLinhaClicada = x;
					}
				}
				
				tr.setBackgroundColor(Color.GRAY);
			}
        	  
          });  
              
          mTableLayout.addView(tr,new TableLayout.LayoutParams(
              LayoutParams.FILL_PARENT,
              LayoutParams.WRAP_CONTENT));
		
	}
	
	public void removerLinha(int index){
		mTableLayout.removeViewAt(mLinhaClicada);
		mLinhaClicada = -1;
	}
	
}
